﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Note
{
    public static class FileOperation
    {
        public static void CreateFolder(this object caller, string dirPath)
        {
            try
            {
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                    Logger.Info(caller.GetType(), dirPath + " Created.");
                }
            }
            catch (Exception e)
            {
                Logger.Error(caller.GetType(), e.Message);
            }
        }

        public static void WriteFile(this object caller, string filePath, string content)
        {
            try
            {
                var parentPath = Path.GetDirectoryName(filePath);
                if (!Directory.Exists(parentPath))
                {
                    Directory.CreateDirectory(parentPath);
                }
                File.WriteAllText(filePath, content, Encoding.UTF8);
            }
            catch (Exception e)
            {
                Logger.Error(caller.GetType(), e.Message);
            }
        }

        public static string ReadFile(this object caller, string filePath)
        {
            try
            {
                if (!File.Exists(filePath))
                {
                    return "";
                }
                return File.ReadAllText(filePath, Encoding.UTF8);
            }
            catch (Exception e)
            {
                Logger.Error(caller.GetType(), e.Message);
            }
            return "";
        }

        public static void DeleteFile(this object caller, string filePath)
        {
            try
            {
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                    Logger.Info(caller.GetType(), filePath + " Delete.");
                }
            }
            catch (Exception e)
            {
                Logger.Error(typeof(FileOperation), e.Message);
            }
        }

        public static void DeleteFolder(this object caller, string filePath, bool deleteAll = true)
        {
            try
            {
                if (Directory.Exists(filePath))
                {
                    Directory.Delete(filePath, deleteAll);
                }
            }
            catch (Exception e)
            {
                Logger.Error(caller.GetType(), e.Message);
            }
        }

        public static void MoveFile(this object caller, string sourceFile, string destFile, bool overwrite = false)
        {
            if (File.Exists(destFile) && !overwrite)
            {
                Logger.Info(caller.GetType(), string.Format("Skip file move from {0} to {1}", sourceFile, destFile));
            }
            else
            {
                try
                {
                    File.Move(sourceFile, destFile);
                    Logger.Info(caller.GetType(), string.Format("File move from {0} to {1}", sourceFile, destFile));
                }
                catch (Exception e)
                {
                    Logger.Error(caller.GetType(), string.Format("Fail to move from {0} to {1} : {2}", sourceFile, destFile, e));
                }
            }
        }

        public static void CopyFile(this object caller, string sourceFile, string destFile, bool overwrite = false)
        {
            if (File.Exists(destFile) && !overwrite)
            {
                Logger.Info(caller.GetType(), string.Format("Skip file copy from {0} to {1}", sourceFile, destFile));
            }
            else
            {
                try
                {
                    File.Copy(sourceFile, destFile, true);
                    Logger.Info(caller.GetType(), string.Format("File copy from {0} to {1}", sourceFile, destFile));
                }
                catch (Exception e)
                {
                    Logger.Error(caller.GetType(), string.Format("Fail to copy from {0} to {1}: {2}", sourceFile, destFile, e));
                }
            }
        }
    }
}
